package cloud.estate.house.service.impl;

import cloud.estate.commons.bean.House;
import cloud.estate.commons.handler.exception.*;
import cloud.estate.commons.mapper.HouseMapper;
import cloud.estate.commons.util.JsonResult;
import cloud.estate.commons.util.ResultTool;
import cloud.estate.house.service.houseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class houseServiceImpl extends ServiceImpl<HouseMapper, House> implements houseService {
    @Override
    public JsonResult addOneHouse(House house){
        try {
            boolean save = save(house);
            if (save){
                return  ResultTool.success("添加成功");
            }
            return ResultTool.fail(501,"添加失败");
        } catch (Exception e) {
            throw new AddOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult deleteOneHouse(String id){
        try {
            boolean removeById = removeById(id);
            if (removeById){
                return  ResultTool.success("删除成功");
            }
            return ResultTool.fail(502,"删除失败");
        } catch (Exception e) {
            throw new DealOneByIdException(e.getMessage());
        }
    }
    @Override
    public JsonResult updateOneHouse(House house){
        try {
            boolean updateById = updateById(house);
            if (updateById){
                return  ResultTool.success("更新成功");
            }
            return ResultTool.fail(503,"更新失败");
        } catch (Exception e) {
            throw new UpdateOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult getOneHouse(String id){
        try {
            House house = getById(id);
            if (house!=null){
                return  ResultTool.success(house);
            }
            return ResultTool.fail(504,"查询失败");
        } catch (Exception e) {
            throw new FindOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult listAllHouse(){
        try {
            return  ResultTool.success(list());
        } catch (Exception e) {
            throw new FindAllException(e.getMessage());
        }
    }
}
